package com.onlyMe.world.tiling
{
	import com.flesler.componentas.core.Destroyable;
	import com.onlyMe.geom.Coord;
	import com.qb9.flashlib.geom.Size;
	
	public class TileGrid implements Destroyable
	{
		private var _size:Size;
		private var matrix:Array = [];
		
		public function TileGrid()
		{
		}

		public function get width():uint
		{
			return matrix.length;
		}
		
		public function get height():uint
		{
			var max:uint = 0;
			for each (var col:Array in matrix)
				if (col.length > max)
					max = col.length;
			return max;
		}
		
		public function getTileAtCoord(coord:Coord):Tile
		{
			return getTileAt(coord.x, coord.y);
		}
		
		public function getTileAt(x:int, y:int):Tile
		{
			return (matrix[x] && matrix[x][y]) as Tile;
		}
		
		public function setTileAt(x:int, y:int, tile:Tile):void
		{
			if (!matrix[x])
				matrix[x] = [];
			matrix[x][y] = tile;
		}
		
		public function get isDestroyed():Boolean
		{
			return matrix === null;
		}
		
		public function findAll():Array
		{
			var all:Array = [];
			for each (var col:Array in matrix)
				for each (var tile:Tile in col)
					all.push.apply(all, tile.children);
			return all;
		}
		
		public function destroy():void
		{
			for each (var col:Array in matrix)
				for each (var tile:Tile in col)
					tile.destroy();
					
			matrix = null;
		}

	}
}
